package sharemem

import (
	"server_cluster/battle/battle_config"
	"server_cluster/common/config"
	"server_cluster/common/logger"
	"server_cluster/common/protoc/pb"
)

const (
	BATTLE_ROLE					= 4		//上阵角色数量
	BATTLE_ROLE_ASSIST			= 2		//支援角色数量
	BATTLE_MONSTER				= 4		//怪物数量
)

type FightAction struct {
	id				int32
    root_time       int32
    node_time       int32
    trigger_type    int8
    trigger         int8
    target_type     int8
    target          int8
    card            int32
    num             int8
}

func (a *FightAction)Meminit() {
	a.id = 0
	a.root_time = 0
	a.node_time = 0
	a.trigger_type = 0
	a.trigger = 0
	a.target_type = 0
	a.target = 0
	a.card = 0
	a.num = 0
}

func (a *FightAction)IsUse() bool {
	return a.id > 0
}

type Fights struct {
    fid      int32
    //怪物
    monster [BATTLE_MONSTER]ShmUnit
}

func (f *Fights) meminit() {
    var i int8
    f.fid = 0
    for i = 0; i < BATTLE_MONSTER; i++ {
        f.monster[i].meminit(i)
    }
}

func (f *Fights) IsUse() bool {
    return f.fid > 0
}
//
type GameData struct {
	bid			int32				//战斗组id
	action_num	int32				// 操作数量
	action		[battle_config.FIGHT_ACTION_MAX]FightAction
	// 角色
	role [BATTLE_ROLE]ShmUnit
	// 支援角色
	role_assist [BATTLE_ROLE_ASSIST]ShmUnit
	//
	ft          [battle_config.ITEM_FIGHT_MAX]Fights	// 战斗组
	fight_num	int8				// 战斗数量
	end_time	int32				// 战场结束时间
	//
	is_active   bool				// 是否主动入战
	create_time int64				// 创建时间-随机种子
	captain_skill int32				// 队长技
}

func (g *GameData)ActionInit() {
	var i int32
	for i = 0; i < battle_config.FIGHT_ACTION_MAX; i++ {
        g.action[i].Meminit()
    }
	g.action_num = 0
}

func (g *GameData)meminit() {
	var i int8
	g.bid = 0
	g.ActionInit()
	for i = 0; i < BATTLE_ROLE; i++ {
		g.role[i].meminit(i)
	}
	for i = 0; i < BATTLE_ROLE_ASSIST; i++ {
		g.role_assist[i].meminit(i)
	}
	for i = 0; i < battle_config.ITEM_FIGHT_MAX; i++ {
		g.ft[i].meminit()
	}
	g.fight_num = 0
	g.is_active = false
	g.create_time = 0
	g.captain_skill = 0
}

func (g *GameData)GetBid() int32 {
	return g.bid
}
//
func (u *GameData) CreateFight(fdata *pb.ServerUserFightData) uint16 {
	// init
	u.meminit()
	//logger.Info("create fight data = %v", fdata)

	fight_role := fdata.GetFightRole()
	fight_role_count := len(fight_role)
	if fight_role_count <= 0 || fight_role_count > BATTLE_ROLE {
		logger.Error("fight role count error %d",fight_role_count)
		return config.PROTO
	}
	assist_role := fdata.GetAssistRole()
	assist_role_count := len(assist_role)
	if assist_role_count > BATTLE_ROLE_ASSIST {
		logger.Error("assist role count error %d",assist_role_count)
		return config.PROTO
	}
	u.bid = fdata.GetBattleGroupId()
	bg := Get_config_battle_group(u.bid)
	if bg == nil {
		logger.Error("get config battle group nil bid = %d",fdata.GetBattleGroupId())
		return config.PROTO
	}
	var i int8
	fids := make([]int32,0)
	for i=0;i<BATTLE_GROUP_BATTLE;i++ {
		if bg.Battle[i] <= 0 {
			break
		}
		fids = append(fids,bg.Battle[i])
	}

	u.fight_num = int8(len(fids))
	if u.fight_num <= 0 {
		logger.Error("fight num error %d",u.fight_num)
		return config.PROTO
	}
	u.end_time = bg.End_time
	u.is_active = fdata.GetStatus()
	u.create_time = fdata.GetTimestamp()
	if u.create_time <= 0 {
		logger.Error("create time error %d",u.create_time)
		return config.PROTO
	}
	//特殊处理
	return config.OK
	u.captain_skill = fdata.GetCaptainSkill()
	if u.captain_skill > 0 && Get_config_captain(u.captain_skill) == nil{
		logger.Error("cfg captain nil %d",u.captain_skill)
		return config.CFGERROR
	}
	// 支援角色不能是上场角色
	if assist_role_count > 0 {
		// role_assist
		for _, v1 := range assist_role {
			if v1.Site < 0 || v1.Site >= BATTLE_ROLE_ASSIST || !u.role_assist[v1.Site].BuildByPb(UNIT_TYPE_ROLE_ASSIST,v1){
				return config.PROTO
			}
		}
	}
	// role
	for _, v1 := range fight_role {
		if v1.Site < 0 || v1.Site >= BATTLE_ROLE || !u.role[v1.Site].BuildByPb(UNIT_TYPE_ROLE,v1){
			return config.PROTO
		}
	}
	// ft
	for k, v := range fids {
		cfg_battle := Get_config_battle(v)
		if cfg_battle == nil {
			logger.Error("cfg_battle nil %d", v)
			return config.CFGERROR
		}
		u.ft[k].fid = cfg_battle.Id
		for i = 0; i < BATTLE_MONSTER; i++ {
			if cfg_battle.Monster[i] <= 0 {
				continue
			}
			cfg_monster := Get_config_monster(cfg_battle.Monster[i])
			if cfg_monster == nil {
				logger.Error("cfg_monster nil %d", cfg_battle.Monster[i])
				return config.CFGERROR
			}
			if !u.ft[k].monster[i].BuildMonster(cfg_monster){
				return config.CFGERROR
			}
		}
	}
	// 数据导入完毕
	// 返回消息
	return config.OK
}

